﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace DIProject.ReceptionGoods
{	public partial class SupplierReturnRpt0 : Form
	{	private int docId;
		private string textFormat;

		public SupplierReturnRpt0() : this(0)
		{
		}
		public SupplierReturnRpt0(int docId)
		{	
            InitializeComponent();
			this.docId = docId;
			textFormat = Text;
		}

		private DIProject.DataSets.Reports reports = new DIProject.DataSets.Reports();
		private DIProject.DataSets.ReportsTableAdapters.ShortageReport_InfoTableAdapter infoTableAdapter = new DIProject.DataSets.ReportsTableAdapters.ShortageReport_InfoTableAdapter();
		private DIProject.DataSets.ReportsTableAdapters.ShortageReport_DataTableAdapter dataTableAdapter = new DIProject.DataSets.ReportsTableAdapters.ShortageReport_DataTableAdapter();
		private DIProject.Reports.ShortageReport.DataSet shortageDataSet = new DIProject.Reports.ShortageReport.DataSet();

		private void SupplierReturnRpt0_Load(object sender, EventArgs e)
		{	try
			{	ShortageReport.FileName = Path.Combine(ConfigurationManager.AppSettings["ReportsPath"], "ShortageReport/CrystalReport.rpt");
				ShortageReport.Load();
			}
			catch(SqlException eq)
			{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void ShortageReport_InitReport(object sender, EventArgs e)
		{	try
			{	ShortageReport.SetDataSource(shortageDataSet);
			}
			catch(SqlException eq)
			{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void ShortageReport_RefreshReport(object sender, EventArgs e)
        {
            try
            {
                shortageDataSet.Clear();
                infoTableAdapter.Fill(reports.ShortageReport_Info, docId);
              /*  for (int i = 0, n = reports.ShortageReport_Info.Count; i < n; ++i)*/
                shortageDataSet.Info.Rows.Add(reports.ShortageReport_Info[0].ItemArray);
                dataTableAdapter.Fill(reports.ShortageReport_Data, docId);
                for (int i = 0, n = reports.ShortageReport_Data.Count; i < n; ++i)
                    shortageDataSet.Data.Rows.Add(reports.ShortageReport_Data[i].ItemArray);
                if (reports.ShortageReport_Info.Count > 0)
                {
                    DIProject.DataSets.Reports.ShortageReport_InfoRow info = reports.ShortageReport_Info[0];
                    Text = string.Format(textFormat, info.Number, info.Date);
                }
            }
            catch (SqlException eq)
            {
                MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
		}
	}
}